package com.whzykj.ctrl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.jfinal.core.Controller;
import com.jfinal.kit.HashKit;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.upload.UploadFile;
import com.whzykj.common.DateUtil;
import com.whzykj.model.Customer;
import com.whzykj.model.Dictionary;
import com.whzykj.model.Station;

public class StationController extends Controller{
	public void index(){
		this.renderJsp("/admin/system/stationList.jsp");
	}
	
	// 显示所有站点信息列表
	public void listStation(){
		Integer page = this.getParaToInt("page");
		Integer rows = this.getParaToInt("rows");
		Integer stationKind=this.getParaToInt("stationKind");
		String stationProperty=this.getPara("stationProperty");
		String stationName=this.getPara("stationName");
		String stationMaster=this.getPara("stationMaster");
		StringBuffer condition=new StringBuffer(" where 1=1");
		if(stationKind!=null && stationKind!=-1){
			condition.append(" and sta.STATION_KIND="+stationKind);
		}
		if(stationName!=null && stationName.trim().length()>0){
			condition.append(" and sta.STATION_NAME like '%"+stationName.trim()+"%'");
		}
		if(stationMaster!=null && stationMaster.trim().length()>0){
			condition.append(" and ui2.USERNAME like'%"+stationMaster.trim()+"%'");
		}
		if(stationProperty!=null && !stationProperty.equals("-1")){
			condition.append(" and sta.STATION_PROPERTY='"+stationProperty+"'");
		}
		String sqlBefore = " SELECT sta.*,ui1.USERNAME as CHARGE_NAME,ui2.USERNAME as MASTER_NAME,dict.DICT_NAME as PROPERTY_NAME ";
		StringBuffer sqlAfter = new StringBuffer(
				" FROM station sta inner JOIN userinfo ui1 on sta.AREA_CHARGE=ui1.USERID ");
		sqlAfter.append(" INNER JOIN userinfo ui2 ON sta.STATION_MASTER=ui2.USERID ");
		sqlAfter.append(" INNER JOIN dictionary dict ON sta.STATION_PROPERTY=dict.DICT_CODE");
		sqlAfter.append(condition.toString());
		sqlAfter.append(" order by sta.CREATE_TIME desc ");
		Page<Station> paginate = Station.dao.paginate(page, rows, sqlBefore,
				sqlAfter.toString());
		Map map = new HashMap();
		map.put("rows", paginate.getList());
		map.put("total", paginate.getTotalRow());
		this.renderJson(map);
	}
	
	/**
	 * 进入添加新站点页面
	 */
	public void addBefore(){
		this.renderJsp("/admin/system/stationAdd.jsp");
	}
	
	/*
	 * 添加站点信息
	 */
	public void addStation(){
		UploadFile upload = getFile("img", "station/");
		try {
			Station station=this.getModel(Station.class);
			if(upload!=null){
				String name = upload.getFile().getName();
				String username = this.getPara("username");
//			String filePath="/upload/"+name;
				String imgUrl=PropKit.use("config.properties").get("web.url")+"/upload/station/"+name;	
				station.set("IMG_URL", imgUrl);
			}
			station.set("STATION_AREA", "湖北省武汉市");
			station.set("CREATE_TIME", DateUtil.getNow());
			if(station.getStr("INIT_PASSWORD")!=null && !station.getStr("INIT_PASSWORD").trim().equals("")){
				station.set("INIT_PASSWORD", station.getStr("INIT_PASSWORD"));
				station.set("STATION_PASSWORD", HashKit.md5(station.getStr("INIT_PASSWORD")));
			}
			boolean add=station.save();
			this.setAttr("addMsg", add?"[系统消息：站点添加成功]":"[系统消息：站点添加失败]");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		this.renderJsp("/admin/system/stationAdd.jsp");
	}
	
	/**
	 * 查询对应站点类型的所有站点
	 */
	public void findStationByKind(){
		Integer kind=this.getParaToInt(0);
		List<Station> list=Station.dao.find("select * from station where STATION_KIND="+kind+" order by STATION_NAME");
		Station station=new Station();
		station.set("STATION_ID", -1).set("STATION_NAME", "--- 请选择站点 ---");
		list.add(0, station);
		this.renderJson(list);
	}
	
	/**
	 * 根据传入的站点市场属性信息获取该属性下所有的站点信息
	 */
	public void findStationByProperty(){
		String prop=this.getPara(0);
		StringBuffer sql=new StringBuffer(" SELECT * from station");
		if(prop!=null){
			sql.append(" where STATION_PROPERTY='"+prop+"' ");
		}
		sql.append("  order by STATION_NAME");
		List<Station> list=Station.dao.find(sql.toString());
		Station station=new Station();
		station.set("STATION_ID", -1).set("STATION_NAME", "--- 请选择站点 ---");
		list.add(0, station);
		this.renderJson(list);
	}
	
	/**
	 * 查询所有站点的市场属性列表
	 */
	public void findStationProperty(){
		List<Dictionary> list=Dictionary.me.find("SELECT * from dictionary where DICT_TYPE='STATION_PROPERTY'");
		Dictionary dict=new Dictionary();
		dict.set("DICT_CODE", "-1").set("DICT_NAME", "--- 请选择站点市场属性 ---");
		list.add(0,dict);
		this.renderJson(list);
	}
	
	/**
	 * 删除站点信息
	 */
	public void remove(){
		Integer stationid=this.getParaToInt("stationid");
		boolean del=Station.dao.deleteById(stationid);
		this.renderText(del?"1":"0");	
	}
	
	/**
	 * 进入到编辑站点的页面
	 */
	public void editBefore(){
		Integer stationid=this.getParaToInt(0);
		Station station=Station.dao.findById(stationid);
		this.setAttr("station", station);
		this.renderJsp("/admin/system/stationEdit.jsp");
	}
	
	/**
	 * 编辑站点信息
	 */
	public void editStation(){
		UploadFile upFile=getFile("img", "station/");	
		//	文件重命名这样用：    getFile().getFile().renameTo(new File(newFileNameHere)); 
		Station station=this.getModel(Station.class);
		if(upFile!=null){
			String fileName=upFile.getFileName();
			String imgUrl=PropKit.use("config.properties").get("web.url")+"/upload/station/"+fileName;		
			station.set("IMG_URL", imgUrl);
		}
		station.update();
//		this.renderJsp("/admin/system/stationList.jsp");
		this.forwardAction("/admin/station/editBefore/"+station.getInt("STATION_ID"));
	}
}
